<!DOCTYPE html>
<html>
<head>
    <title>Elevator Saga performance tests</title>
    <meta charset="UTF-8">

    <style>
        body { font-family: Arial, Helvetica, sans-serif;}
    </style>

    <script type="text/javascript" src="../libs/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="../libs/lodash.min.js"></script>
    <script type="text/javascript" src="../libs/riot.js"></script>

    <script type="text/javascript" src="../libs/unobservable.js"></script>

    <script type="text/javascript" src="../base.js"></script>
    <script type="text/javascript" src="../movable.js"></script>
    <script type="text/javascript" src="../floor.js"></script>
    <script type="text/javascript" src="../user.js"></script>
    <script type="text/javascript" src="../elevator.js"></script>
    <script type="text/javascript" src="../interfaces.js"></script>
    <script type="text/javascript" src="../world.js"></script>
    <script type="text/javascript" src="../presenters.js"></script>
    <script type="text/javascript" src="../challenges.js"></script>
</head>
<body>
    <h3>Movable</h3>
    <div id="results_movable"><h4></h4></div>
    <h3>Elevator</h3>
    <div id="results_elevator"><h4></h4></div>
    <h3>User</h3>
    <div id="results_user"><h4></h4></div>
    <h3>Floor</h3>
    <div id="results_floor"><h4></h4></div>


    <script type="text/javascript">

        var now = (function() {
         
          // Returns the number of milliseconds elapsed since either the browser navigationStart event or 
          // the UNIX epoch, depending on availability.
          // Where the browser supports 'performance' we use that as it is more accurate (microsoeconds
          // will be returned in the fractional part) and more reliable as it does not rely on the system time. 
          // Where 'performance' is not available, we will fall back to Date().getTime().
         
          // jsFiddle: http://jsfiddle.net/davidwaterston/xCXvJ
         
         
          var performance = window.performance || {};
            
          performance.now = (function() {
            return performance.now    ||
            performance.webkitNow     ||
            performance.msNow         ||
            performance.oNow          ||
            performance.mozNow        ||
            function() { return new Date().getTime(); };
          })();
                  
          return performance.now();         
         
        });

        // First blow through the allocation system to make first test more predictable..
        for(var i=1000; i>0; i--) {
            var m = new Movable();
            var e = new Elevator(1.5, 4, 40);
            var u = new User(10, 10, 40);
        }

        var measure = function(name, numIterations, fn) {
            var t0 = now();
            for(var i=numIterations; i>0; i--) {
                fn();
            }
            var t1 = now();
            var report = name + " " + numIterations * 0.001 + "k times: " + (t1 - t0).toFixed(3) + " ms";
            return report;
        };

        $("#results_movable h4").append(measure("Creation", 10000, function() { var m = new Movable() }));
        $("#results_elevator h4").append(measure("Creation", 10000, function() { var e = new Elevator(1.5, 4, 40) }));
        $("#results_user h4").append(measure("Creation", 10000, function() { var u = new User(10, 10, 40) }));

    </script>
</body>
